package com.example.dnm.controller;

import com.example.dnm.pojo.Role;
import com.example.dnm.pojo.User;
import com.example.dnm.resp.JsonResult;
import com.example.dnm.resp.JsonResultUtil;
import com.example.dnm.service.UserService;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import netscape.javascript.JSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("findAllUser")
    public JsonResult finduser(User user) {
        try {
            List<User> list = userService.finduser(user);
            PageInfo<User> pageInfo = new PageInfo<>(list);
            return JsonResultUtil.getSuccessJson("查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResultUtil.getSuccessJson("添加失败");
    }


    @RequestMapping("addUser")
    public JsonResult addUser(User user) {
        try {

            if (user.getUsername() == null) {
                return JsonResultUtil.getErrorJson("用户名为空");
            }
            if (user.getPassword() == null) {
                return JsonResultUtil.getErrorJson("密码为空");
            }
            User userByName = userService.findUserByName(user);
            if (userByName != null) {
                return JsonResultUtil.getErrorJson("用户名重复");
            }
            userService.addUser(user);
            return JsonResultUtil.getSuccessJson("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResultUtil.getErrorJson("添加失败");
    }

//
    @RequestMapping("updateUser")
    public JsonResult updateUser(User user) {
        try {
            if (user.getUsername() == null) {
                return JsonResultUtil.getErrorJson("用户名为空");
            }
            if (user.getPassword() == null) {
                return JsonResultUtil.getErrorJson("密码为空");
            }
            User userByName = userService.findUserByName(user);
            if (userByName != null && !userByName.getId().equals(user.getId())) {
                return JsonResultUtil.getErrorJson("用户名重复");
            }
            userService.updateUser(user);
            return JsonResultUtil.getSuccessJson("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResultUtil.getErrorJson("更新失败");
    }
//
    @RequestMapping("deleteUser")
    public JsonResult deleteUser(User user) {

        try {
            userService.deleteUser(user);
            return JsonResultUtil.getSuccessJson("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResultUtil.getErrorJson("删除失败");
    }

    @RequestMapping("selectThree")
    public JsonResult selectThree(User user) {
        try {
            List<User> list = userService.selectThree(user);
            PageInfo<User> userPageInfo = new PageInfo<>(list);
            return JsonResultUtil.getSuccessJson("查询成功", userPageInfo);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return JsonResultUtil.getErrorJson("查询失败");
    }
//
//    @RequestMapping("lianbiao")
//    public JsonResult lianbiao(User user) {
//        List<User> list = userService.lianbiao();
//        return JsonResultUtil.getSuccessJson("查询成功", list);
//    }
}


